//
//  StudyUserCourseTableViewCell.m
//  TeacherNet
//
//  Created by slz on 2017/12/15.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "StudyUserCourseTableViewCell.h"

@implementation StudyUserCourseTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        _courseImageView = [UIImageView new];
        _courseImageView.backgroundColor = kLight_TextColor_Default;
        _courseImageView.contentMode = UIViewContentModeScaleAspectFill;
        _courseImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:_courseImageView];
        [_courseImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(15);
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(100, 55));
        }];
        
        _courseLabel = [UILabel new];
        _courseLabel.textColor = kDark_TextColor_Default;
        _courseLabel.fontSize = kFont_Size_Small_Title;
        [self.contentView addSubview:_courseLabel];
        [_courseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_courseImageView.mas_right).offset(5);
            make.top.mas_equalTo(_courseImageView.mas_top).offset(2);
            make.trailing.mas_equalTo(self.contentView).offset(-15);
        }];
        
        CGFloat  fullProgressWidth = kScreenWidth * .40;
        
        CALayer * progressBackLayer = [CALayer new];
        progressBackLayer.cornerRadius = 3;
        progressBackLayer.masksToBounds = YES;
        progressBackLayer.left = 120;
        progressBackLayer.centerY = 42;
        progressBackLayer.size = CGSizeMake(fullProgressWidth, 8);
        progressBackLayer.backgroundColor = [UIColor colorWithARGB:0xDEDFE0].CGColor;
        [self.contentView.layer addSublayer:progressBackLayer];
        
        _endTime = [UILabel new];
        _endTime.fontSize = kFont_Size_Cotent_Middle;
        _endTime.textColor = kLight_TextColor_Default;
        [self.contentView addSubview:_endTime];
        [_endTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_courseLabel.mas_left);
            make.bottom.mas_equalTo(_courseImageView.mas_bottom).offset(-2);
        }];
        
        _progressLabel = [UILabel new];
        _progressLabel.textColor = kDark_TextColor_Default;
        _progressLabel.fontSize = kFont_Size_Cotent_Middle;
        _progressLabel.left = progressBackLayer.right + 10;
        _progressLabel.size = CGSizeMake(50, 25);
        _progressLabel.centerY = progressBackLayer.centerY;
        [self addSubview:_progressLabel];
        
        _progressLayer = [CALayer new];
        _progressLayer.cornerRadius = 3;
        _progressLayer.masksToBounds = YES;
        _progressLayer.backgroundColor = kButtonColorBlue.CGColor;
        _progressLayer.height = progressBackLayer.height;
        _progressLayer.origin = CGPointMake(0, 0);
        [progressBackLayer addSublayer:_progressLayer];
        
        _courseCount = [UILabel new];
        _courseCount.textAlignment = NSTextAlignmentRight;
        _courseCount.textColor = kDark_TextColor_Default;
        _courseCount.fontSize = kFont_Size_Cotent_Middle;
        [self.contentView addSubview:_courseCount];
        [_courseCount mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.contentView.mas_right).offset(-15);
            make.bottom.mas_equalTo(_endTime.mas_bottom);
        }];
        
        UIView * lineView = [UIView quickLineColorView];
        [self.contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.and.bottom.mas_equalTo(self.contentView);
            make.height.mas_equalTo(.5);
        }];
    }
    return self;
}

-(void)setWithModel:(StudyCourseModel *)model
{
    [_courseImageView sd_setImageWithURL:[NSURL URLWithString:kAPI_IMAGE_HOST(model.courseImage)] randomPlaceHolder:YES];
    _courseLabel.text = model.courseName;
    _endTime.text = [NSString stringWithFormat:@"结束时间:%@",model.endTime];
    CGFloat progress = model.allCourseNumber > 0 ? (float)model.currentCourseNumber/(float)model.allCourseNumber : 0;
    [self updateProgress:progress];
    NSMutableAttributedString * first = [[NSMutableAttributedString alloc]initWithString:@"学时:"];
    NSAttributedString * attString = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",model.courseNumber] attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
    [first appendAttributedString:attString];
    _courseCount.attributedText  =first;
}

- (void)updateProgress:(CGFloat)progress{
    CGFloat  fullProgressWidth = kScreenWidth * .40;
    self.progressLayer.width = fullProgressWidth * progress;
    NSInteger progressInt = progress * 100;
    if (progressInt < 0 || progressInt > 100)
    {
        progressInt = 0;
    }
    NSMutableAttributedString * attString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%ld",progressInt] attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
    [attString appendAttributedString:[[NSAttributedString alloc]initWithString:@"%"]];
    _progressLabel.attributedText = attString;
}

@end
